/*
 * @lc app=leetcode.cn id=14 lang=csharp
 *
 * [14] 最长公共前缀
 */

// @lc code=start
public class Solution {
    public string LongestCommonPrefix(string[] strs) {
        var left = 0;
        var str = strs[0];
        var right = str.Length - 1;
        var index = right / 2;
        while(left <= right)
        {
            var _char = str[index];
            for(int i = 1; i < strs.Length; i++)
            {
                if(index >= strs[i].Length || strs[i][index] != _char)
                {
                    right = index - 1;
                    break;
                }
            }
            // index位全部字符相同
            if(index == left)
            {
                left++;
                index = (left + right) / 2;
            }
            else
            {
                index = (left + index) / 2;
            }
        }
        return str.Substring(0, right + 1);
    }
}
// @lc code=end

